tmp_data_dir = ImageAnalysis.Settings.TempDataDir;

if ~exist('aux_data', 'var')
    disp('aux data not found. loading it...')
        aux_data=load_lvd([tmp_data_dir mouse_id '\Data\' date_id '\' aux_files{1}]);

end
    %load([adata_dir mouse_id '\' date_id '\' adata_file])
    %try to get the sync
    ind=1;
    stim_data = loadstimdata([tmp_data_dir mouse_id '\stim\' date_id '\' stim_files{ind}]);
    tmp_data=load_lvd([tmp_data_dir mouse_id '\Data\' date_id '\' aux_files{ind}]);
    nFramesDiff=abs(length(get_frame_times(tmp_data(1,:)))-nbr_frames(ind));
    for iAuxChan=1:size(tmp_data,1)
        tmp_frames=get_frame_times(tmp_data(iAuxChan,:));
        frame_channel=iAuxChan;
        if abs(length(tmp_frames)-nbr_frames(ind)) < nFramesDiff
            % found a better match
            nFramesDiff = abs(length(tmp_frames)-nbr_frames(ind));
            bestMatch = iAuxChan;
        end
        if nFramesDiff == 0
            %found a perfect match
            disp(['found frame galvo signal on channel ' num2str(frame_channel)]);
            break
        end
    end
    frame_times=tmp_frames;

if size(aux_data,1)==7
    shutter_channel=3;
else
    shutter_channel=4;
end

if sel_roi>0
    tmp1=corrcoef(dx,ROIs(sel_roi).activity);
    tmp2=corrcoef(dy,ROIs(sel_roi).activity);
else
    tmp1=corrcoef(dx,np.activity);
    tmp2=corrcoef(dy,np.activity);
end
hccc=max(abs(tmp1(2)),abs(tmp2(2)));

if ~exist('h_adp_activity','var') || ~ishandle(h_adp_activity)
    figure(1003)
    clf
    mon_pos=get(0,'MonitorPositions');
    set(gcf,'color','k','menubar','none','position',[mon_pos(1) 600 mon_pos(3)-mon_pos(1) 400]);
    %set(gcf,'color','k','menubar','none','position',[mon_pos(1,1) 600 mon_pos(2,3)-mon_pos(1,1) 400]);
    set(gcf,'keypressfcn','plot_aux_data_kpf');
    axes('position',[0 0 1 1])
    hold on
    
    drawnow
    if exist('imeta_data','var')
        [imeta_data]=filter_eye_monitor_data(imeta_data);
    end
    
    velM=diff(aux_data(2,:));
    velM(velM>5)=velM(velM>5)-10;
    velM(velM<-5)=velM(velM<-5)+10;
    velM=ftfil(velM,1000,0,10);
    
    if size(aux_data,1)<7
        aux_data(7,:)=0;
    end
    velP=diff(aux_data(7,:));
    velP(velP>5)=velP(velP>5)-10;
    velP(velP<-5)=velP(velP<-5)+10;
    velP=ftfil(velP,1000,0,10);
    
    velP_ind=((smooth(abs(velP),100))>0.002);
    velM_ind=((smooth(abs(velM),100))>0.002);
    
    plot_id=[2 3];
    
    colorind='rggyb';
    
    % plot stim data
    if exist('stim_data','var')
        stim_sep=strfind(stim_data.seq, '-');
        if ~isempty(stim_sep)
            stim_or = textscan(stim_data.seq(1:stim_sep-1),'%u');
            stim_eye = textscan(stim_data.seq(stim_sep+1:end),'%u');
            stim_or = stim_or{1};
            stim_eye = stim_eye{1};
            if ~(str2num(stim_data.orientations) * str2num(stim_data.repetitions) * 2 == length(stim_or) == length(stim_eye))
                %on_off = zeros(size(aux_data,2));
                ux_data(1,:)=0;
                ux_data(5,:)=0;
                ux_data(4,:)=0;
                on=str2num(stim_data.on);
                off=str2num(stim_data.off);
                for i=1:nbr_frames/(on+off)
                    ux_data(1,frame_times((i-1)* (on+off) + off +1):frame_times((i-1)* (on+off) + off +on)) = 1;
                    ux_data(5,frame_times((i-1)* (on+off)  +1):frame_times((i-1)* (on+off) + off +on)) = stim_eye(i);
                    ux_data(4,frame_times((i-1)* (on+off) +1):frame_times((i-1)* (on+off) + off +on)) = double(stim_or(i))/360;
                    
                end
                %on_off = on_off +4.4;
                %plot(aux_data(1,:) + 4.4, 'y');
                area(20*(ux_data(1,:)),'linestyle','none','facecolor',[0 1 0]*0.25);
                %area(20*(velM_ind&~velP_ind),'linestyle','none','facecolor',[0 1 0]*0.15);
                %area(20*(velM_ind&velP_ind),'linestyle','none','facecolor',[1 1 1]*0.1);
                
                plot(ux_data(5,:) + 1.1, 'b');
                plot(ux_data(4,:) + 7.8, 'g');
                %plot orientation numbers from stim file
                nstims = numel(unique(stim_or));
                for i=1:nbr_frames/(on+off)
                    text(frame_times((i-1)*(on+off)+off+1),7,num2str(1+stim_or(i)/(360/nstims)),'color','w','fontsize',15,'fontweight','bold')
                    text(frame_times((i-1)*(on+off)+off+7),6,num2str(stim_or(i)),'color','w','fontsize',11,'fontweight','bold','rotation',90)
                    text(frame_times((i-1)*(on+off)+off+1),5,num2str(stim_eye(i)),'color','w','fontsize',15,'fontweight','bold')
                end
            else
                disp('length of decoded stim orientations does not match');
            end
        else
            disp('stim_data could not be decoded');
            return
        end
    end
    
    
    %area(20*(velP_ind&~velM_ind),'linestyle','none','facecolor',[1 1 0]*0.25);
    %area(20*(velM_ind&~velP_ind),'linestyle','none','facecolor',[0 1 0]*0.15);
    %area(20*(velM_ind&velP_ind),'linestyle','none','facecolor',[1 1 1]*0.1);
    
    %plot orientation sensor
    
    min_laser=median(aux_data(5,:))-std(aux_data(5,:));% min(aux_data(5,round(size(aux_data,2)/10):round(9*size(aux_data,2)/10)));
    max_laser=median(aux_data(5,:))+std(aux_data(5,:));% max(aux_data(5,round(size(aux_data,2)/10):round(9*size(aux_data,2)/10)));
    plot(-(aux_data(5,:)-min_laser)/(max_laser-min_laser)+2.1*(length(plot_id)+2),'color',[0.5 1 0.5]*0.6);
    
    
    %plot air puff and noise tags
    if size(aux_data,1)>7
        plot((aux_data(9,:))/5+0.1,'color',[0 0 0.5]);
        plot((aux_data(8,:))/5+0.1);
    end
    
    for aux_plot_ind=1:length(plot_id) %loop to plot voltage left an right eye shutter %NOTE%
        plot((aux_data(plot_id(aux_plot_ind),:)-min(aux_data(plot_id(aux_plot_ind),:)))/(max(aux_data(plot_id(aux_plot_ind),:))-min(aux_data(plot_id(aux_plot_ind),:)))+1.1*aux_plot_ind-1,'color',colorind(aux_plot_ind));
    end
    
    min_vel=prctile([velP velM],0.01);
    max_vel=prctile([velP velM],0.99);
    
    %if sel_roi>0
    cc=hsv(numel(ROIs));
 for i=1:numel(ROIs)
    h_adp_activity=plot(frame_times,2*(ROIs(i).activity-min(ROIs(i).activity))/(max(ROIs(i).activity)-min(ROIs(i).activity))+1.6*aux_plot_ind,'color',cc(i,:)); %NOTE% raw neuron trace
 end
    %        td= ROIs(sel_roi).activity - np.activity;
        %h_canp_activity=plot(frame_times,2*(td-min(td))/(max(td)-min(td))+2.4*aux_plot_ind,'y'); % plot of neuron activity - neuropil activity %NOTE%
        %ca_activity=ROIs(sel_roi).activity;
 %       ca_activity=td;
 %       on=str2num(stim_data.on);
 %       off=str2num(stim_data.off);
 %       for i=1:nbr_frames/(on+off)
 %           ca_activity((i-1)*(on+off)+1:(i)*(on+off))=ca_activity((i-1)*(on+off)+1:(i)*(on+off))-mean(ca_activity((i-1)*(on+off)+1:(i)*(on+off)-on));
 %       end
        %h_casm_activity=plot(frame_times,2*(ca_activity-min(ca_activity))/(max(ca_activity)-min(ca_activity))+3.3*aux_plot_ind,'r'); % plot of off frames corrected np activity trace %NOTE%
%    else
%        h_adp_activity=plot(frame_times,2*(np.activity-min(np.activity))/(max(np.activity)-min(np.activity))+1.1*aux_plot_ind,'w');
%    end
    
    %plot registration correction
    %plot(frame_times,(dx-min(dx))/(max(dx)-min(dx))+1.1*(aux_plot_ind+2),'b');
    %plot(frame_times,(dy-min(dy))/(max(dy)-min(dy))+1.1*(aux_plot_ind+2),'r');
    
    %plot aud data
    if exist('aud_data','var')
        comp_aud=compress_aud_data(aud_data,aux_data(shutter_channel,:));
        plot((comp_aud-min(comp_aud))/(max(comp_aud)-min(comp_aud))+1.1*(aux_plot_ind+6));
    end
    
    esf=0.8;
    if exist('imeta_data','var')
        nan_eye_ind=~isnan(imeta_data(7,:))&~isnan(imeta_data(8,:));
        if min(min(corrcoef(imeta_data(7,nan_eye_ind),imeta_data(8,nan_eye_ind))))>0
            eye_pos=imeta_data(7,:)+imeta_data(8,:);
        else
            eye_pos=imeta_data(7,:)-imeta_data(8,:);
        end
        plot(iframe_times,(eye_pos-esf*min(eye_pos))/(esf*max(eye_pos)-esf*min(eye_pos))+1.1*(aux_plot_ind+3),'m');
        plot(iframe_times,(imeta_data(9,:)-esf*min(imeta_data(9,:)))/(esf*max(imeta_data(9,:))-esf*min(imeta_data(9,:)))+1.1*(aux_plot_ind+4),'c');
        
        % plot whisker tracker data
        if std(imeta_data(3,:))>0.1
            wskx=imeta_data(3,:);
            wsky=imeta_data(4,:);
            wskx(wskx==0)=NaN;
            wsky(wsky==0)=NaN;
            wsk=diff(sqrt((wskx-nanmedian(wskx)).^2+(wsky-nanmedian(wsky)).^2));
            wsk=[wsk(1) wsk];
            %wsks=smooth(abs(wsk),10);
            plot(iframe_times,(wsk-esf*min(wsk))/(esf*max(wsk)-esf*min(wsk))+1.1*(aux_plot_ind+5),'color',[0.5 0 1]);
            %plot(iframe_times,(wsks-min(wsk))/(max(wsk)-min(wsk))+1.1*(aux_plot_ind+5),'color',[0.5 0 1]);
        end
    end
    
    
    
    %     plot((velM-min_vel)/(max_vel-min_vel)+1.1*(aux_plot_ind+5),'g');
    %     plot((velP-min_vel)/(max_vel-min_vel)+1.1*(aux_plot_ind+6),'y');
    h_aux_txt=text(0,0.95,[' Cell nr. ' num2str(sel_roi) ' - ExpID ' fnames{1}(4:7) ' ' ROIs(sel_roi).type ' - hccc: ' num2str(hccc)],'color','w','units','normalized','fontsize',15,'fontweight','bold');
    axis tight
    ylim([0 1.1*(aux_plot_ind+7)])
    axis off
    %zoom on

else
    if sel_roi>0
        set(h_adp_activity,'ydata',2*(ROIs(sel_roi).activity-min(ROIs(sel_roi).activity))/(max(ROIs(sel_roi).activity)-min(ROIs(sel_roi).activity))+1.1*aux_plot_ind);
        set(h_aux_txt,'string',[' Cell nr. ' num2str(sel_roi) ' - ExpID ' fnames{1}(4:7) ' ' ROIs(sel_roi).type ' - hccc: ' num2str(hccc)]);
        
                %h_adp_activity=plot(frame_times,2*(ROIs(sel_roi).activity-min(ROIs(sel_roi).activity))/(max(ROIs(sel_roi).activity)-min(ROIs(sel_roi).activity))+1.6*aux_plot_ind,'w');
        td= ROIs(sel_roi).activity - np.activity;
       set(h_canp_activity,'ydata',2*(td-min(td))/(max(td)-min(td))+2.4*aux_plot_ind);
        %ca_activity=ROIs(sel_roi).activity;
        ca_activity=td;
        on=str2num(stim_data.on);
        off=str2num(stim_data.off);
        for i=1:nbr_frames/(on+off)
            ca_activity((i-1)*(on+off)+1:(i)*(on+off))=ca_activity((i-1)*(on+off)+1:(i)*(on+off))-mean(ca_activity((i-1)*(on+off)+1:(i)*(on+off)-on));
        end
        set(h_casm_activity,'ydata',2*(ca_activity-min(ca_activity))/(max(ca_activity)-min(ca_activity))+3.3*aux_plot_ind);

        
        
    else
        set(h_adp_activity,'ydata',2*(np.activity-min(np.activity))/(max(np.activity)-min(np.activity))+1.1*aux_plot_ind);
        set(h_aux_txt,'string',[' Cell nr. ' 'np' ' - ExpID ' fnames{1}(4:7) ' '  ' - hccc: ' num2str(hccc)]);
    end
    


end























